package zhu.areas.user.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;

import org.apache.struts2.ServletActionContext;
import zhu.areas.dic.data.FieldType;
import zhu.areas.dic.data.Role;
import zhu.areas.user.data.Capital;
import zhu.areas.user.data.User;
import zhu.areas.user.service.ICapitalService;
import zhu.areas.user.service.IUserService;
import zhu.common.service.IDicService;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class CapitalAction extends ActionSupport{
	
	//注入的Service
	private ICapitalService capitalService;
	private IUserService userService;
	private IDicService dicService;
	
	//持久化对象以及他的外键关联对象
	private Capital capital=new Capital();
	private User user=new User();
	private Role capType;
	private FieldType capField;
	
	//外键
	private int cap_Type;
	private int cap_Field;
	
	//文件上传
	//封装上传文件域的属性
	private File capAttach;
	//封装上传文件类型的属性
	private String capAttachContentType;
	//封装上传文件名的属性
	private String capAttachFileName;
	//直接在配置文件配置的上传文件地址
	private String filePath;
	
	public String changeToAppCapital(){
		return SUCCESS;
	}
	
	public String appCaptial() throws IOException{
		user=userService.findByUsername((String) ServletActionContext.getRequest().getSession().getAttribute("userName"));
		capital.setUser(user);
//		//System.out.println(capital.getCapID());
		capType=(Role) dicService.getValueById("Role","roleID", getCap_Type());
//		System.out.println(capType.getcName());
		capital.setCapType(capType);
//		capital.setCapName("中南民族大学");
//		capital.setConUsername("杨露");
//		capital.setPhone("15827001030");
		capField=(FieldType)dicService.getValueById("FieldType","fieldTypID", getCap_Field());
		capital.setCapField(capField);
//		capital.setAddress("湖北省武汉市");
		capital.setAppTime(new Date());
		capital.setState(0);
		
		//上传文件
		//FileOutputStream fos=new FileOutputStream(getFilePath()+"\\"+getCapAttachFileName());
		FileOutputStream fos=new FileOutputStream("\\"+getCapAttachFileName());
		FileInputStream fis=new FileInputStream(getCapAttach());
		byte[] buffer=new byte[1024];
		int length=0;
		while((length=fis.read(buffer))>0){
			fos.write(buffer, 0, length);
		}
		
		//capital.set
		if(capitalService.addCapital(capital)){
			System.out.println("保存基本信息成功");
			return SUCCESS;
		}
		else{
			return ERROR;
		}
	}
	
	public ICapitalService getCapitalService() {
		return capitalService;
	}
	public void setCapitalService(ICapitalService capitalService) {
		this.capitalService = capitalService;
	}
	public IUserService getUserService() {
		return userService;
	}
	public void setUserService(IUserService userService) {
		this.userService = userService;
	}
	public IDicService getDicService() {
		return dicService;
	}
	public void setDicService(IDicService dicService) {
		this.dicService = dicService;
	}
	public Capital getCapital() {
		return capital;
	}
	public void setCapital(Capital capital) {
		this.capital = capital;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}

	public Role getCapType() {
		return capType;
	}

	public void setCapType(Role capType) {
		this.capType = capType;
	}

	public FieldType getCapField() {
		return capField;
	}

	public void setCapField(FieldType capField) {
		this.capField = capField;
	}

	public int getCap_Type() {
		return cap_Type;
	}

	public void setCap_Type(int cap_Type) {
		this.cap_Type = cap_Type;
	}

	public int getCap_Field() {
		return cap_Field;
	}

	public void setCap_Field(int cap_Field) {
		this.cap_Field = cap_Field;
	}

	public File getCapAttach() {
		return capAttach;
	}

	public void setCapAttach(File capAttach) {
		this.capAttach = capAttach;
	}

	public String getCapAttachContentType() {
		return capAttachContentType;
	}

	public void setCapAttachContentType(String capAttachContentType) {
		this.capAttachContentType = capAttachContentType;
	}

	public String getCapAttachFileName() {
		return capAttachFileName;
	}

	public void setCapAttachFileName(String capAttachFileName) {
		this.capAttachFileName = capAttachFileName;
	}

	@SuppressWarnings("deprecation")
	public String getFilePath() {
		return ServletActionContext.getRequest().getRealPath(filePath);
	}

	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}
	

	
	
}
